<?php

namespace app\controllers;

use yii\data\Pagination;
use yii\db\Query;
use yii\web\Controller;


class WorkController extends AdminBaseController {


    public function actionIndex(){
        if ($this->post()){
            $name=\Yii::$app->request->post()['name'];
            $query=(new Query())->select('*')->from('yii_work')->where(['like','name',$name]);
        }else{
            $query=(new Query())->select('*')->from('yii_work');
        }

        $pages=new Pagination(['totalCount' =>$query->count()]);
        $pages->defaultPageSize = 10;//每页10条
        $list=$query->offset($pages->offset)->limit($pages->limit)->all();
        foreach ($list as $k=>$v){
            $list[$k]['education']=$this->Geteducation($v['education']);
            $list[$k]['city']=$this->Getregion($v['city']);
            $list[$k]['region']=$this->Getregion($v['region']);
            $list[$k]['experience']=$this->Getexperience($v['experience']);
            $list[$k]['company']=$this->GetCompany($v['company']);
            $list[$k]['address']=json_decode($list[$k]['address'],true);
        }
        return $this->render('index',['list'=>$list,'pages'=>$pages]);

    }


    //投递列表
    public function actionMember(){
        if ($this->post()){
            $name=\Yii::$app->request->post()['name'];
            $query=(new Query())->select('*')->from('yii_user_work')->where(['like','name',$name]);
        }else{
            $query=(new Query())->select('*')->from('yii_user_work');
        }

        $pages=new Pagination(['totalCount' =>$query->count()]);
        $pages->defaultPageSize = 10;//每页10条
        $list=$query->offset($pages->offset)->limit($pages->limit)->all();
        foreach ($list as $k=>$v){
            $list[$k]['userinfo']=$this->GetUser($v['user_id']);
            $list[$k]['work']=$this->GetWork($v['work_id']);
        }

        return $this->render('member',['list'=>$list,'pages'=>$pages]);
    }

    //删除投递列表
    public function actionDelmember(){
        $id=$this->PostId();
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            \Yii::$app->db->createCommand()->delete('yii_user_work',array('id'=>$id))->execute();
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //删除全部投递列表
    public function actionDelallmember(){
        $id=\Yii::$app->request->post()['id'];
        $arr=explode(',',$id);
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            \Yii::$app->db->createCommand()->delete('yii_user_work',['in','id',$arr])->execute();
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }


    //查看简历
    public function actionLook($user_id){
         $where['user_id']=$user_id;
         //教育经历
         $education=(new Query())->select('*')->from('yii_user_education')->where($where)->all();
         //工作经历
         $experience=(new Query())->select('*')->from('yii_user_experience')->where($where)->all();
         //工作期望
         $hope=(new Query())->select('*')->from('yii_user_hope')->where($where)->all();
         //个人评价
         $evaluate=(new Query())->select('*')->from('yii_user_evaluate')->where($where)->one();

        return $this->render('look',['education'=>$education,'experience'=>$experience,'hope'=>$hope,'evaluate'=>$evaluate]);
    }

    //添加
    public function actionAdd(){
        if ($this->post()){
            $data=\Yii::$app->request->post();
            $data['create_time']=time();
            $data['update_time']=time();
            $data['address']=json_encode($data['address']);
            $res=\Yii::$app->db->createCommand()->insert('yii_work',$data)->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'添加成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'添加失败']);
            }
        }
         $compay=(new Query())->select('id,name')->from('yii_company')->all();
         $education=(new Query())->select('id,name')->from('yii_education')->all();
         $experience=(new Query())->select('id,name')->from('yii_experience')->all();
         return $this->render('add',['company'=>$compay,'education'=>$education,'experience'=>$experience]);
    }


    //编辑
    public function actionEdit(){
        if ($this->post()){
            $data=\Yii::$app->request->post();
            $data['update_time']=time();
            $data['address']=json_encode($data['address']);
            $res=\Yii::$app->db->createCommand()->update('yii_work',$data,['id'=>$data['id']])->execute();
            if ($res){
                return json_encode(['code'=>1,'msg'=>'更新成功']);
            }else{
                return json_encode(['code'=>0,'msg'=>'更新失败']);
            }
        }
        $id=$this->GetId();
        $list=(new Query())->select('*')->from('yii_work')->where(['id'=>$id])->one();
        $list['address']=json_decode($list['address'],true);
        $compay=(new Query())->select('id,name')->from('yii_company')->all();
        $education=(new Query())->select('id,name')->from('yii_education')->all();
        $experience=(new Query())->select('id,name')->from('yii_experience')->all();
        return $this->render('edit',['list'=>$list,'company'=>$compay,'education'=>$education,'experience'=>$experience]);
    }

    //删除
    public function actionDel(){
        $id=$this->PostId();
        $transaction = \Yii::$app->db->beginTransaction();
        try {
             \Yii::$app->db->createCommand()->delete('yii_work',array('id'=>$id))->execute();
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

    //全部删除
    public function actionDelall(){
        $id=\Yii::$app->request->post()['id'];
        $arr=explode(',',$id);

        $transaction = \Yii::$app->db->beginTransaction();
        try {
            \Yii::$app->db->createCommand()->delete('yii_work',['in','id',$arr])->execute();
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }

     //获取地区
    public function actionRegion(){
        if ($this->post()){
         $data=\Yii::$app->request->post();
         if (!empty($data['city'])){
             $list=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$data['city']])->all();
             $region=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$list[0]['id']])->all();
             return json_encode(['list'=>$list,'region'=>$region]);
         }else if (!empty($data['region'])){
             $list=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$data['region']])->all();
             return json_encode($list);
         }

        }else{
            $province=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>0])->all();
            $city=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$province[0]['id']])->all();
            $region=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$city[0]['id']])->all();
            return json_encode(['province'=>$province,'city'=>$city,'region'=>$region]);
        }
    }

    //获取地区
    public function actionRegion2(){
        if ($this->post()){
            $data=\Yii::$app->request->post();
            if (!empty($data['city'])){
                $city=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$data['city']])->all();
                $region=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$city[0]['id']])->all();
                return json_encode(['city'=>$city,'region'=>$region]);
            }else if (!empty($data['region'])){
                $list=(new Query())->select('id,name')->from('yii_region')->where(['pid'=>$data['region']])->all();
                return json_encode($list);
            }

        }
    }

   //查看招聘岗位
   public function actionLook2($id){
   	 $where['company']=$id;
   	 $query=(new Query())->select('*')->from('yii_work')->where($where);
   	 $pages=new Pagination(['totalCount' =>$query->count()]);
     $pages->defaultPageSize = 10;//每页10条
     $list=$query->offset($pages->offset)->limit($pages->limit)->all();
        foreach ($list as $k=>$v){
            $list[$k]['education']=$this->Geteducation($v['education']);
            $list[$k]['city']=$this->Getregion($v['city']);
            $list[$k]['region']=$this->Getregion($v['region']);
            $list[$k]['experience']=$this->Getexperience($v['experience']);
            $list[$k]['company']=$this->GetCompany($v['company']);
            $list[$k]['address']=json_decode($list[$k]['address'],true);
        }
   	        return $this->render('look2',['list'=>$list,'pages'=>$pages]);
   }


    //获取学历
    public function Geteducation($id){
        $list=(new Query())->select('name')->from('yii_education')->where(['id'=>$id])->one();
        return $list['name'];
    }

    //获取地区
    public function Getregion($id){
        $list=(new Query())->select('name')->from('yii_region')->where(['id'=>$id])->one();
        return $list['name'];
    }

    //获取经验
    public function Getexperience($id){
        $list=(new Query())->select('name')->from('yii_experience')->where(['id'=>$id])->one();
        return $list['name'];
    }

    //获取公司
    public function GetCompany($id){
        $list=(new Query())->select('name')->from('yii_company')->where(['id'=>$id])->one();
        return $list['name'];

    }


}
